package com.love.usedcar.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.love.usedcar.domain.dto.CityNodeDTO;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.love.usedcar.mapper.CityDirectoryMapper;
import com.love.usedcar.domain.CityDirectory;
import com.love.usedcar.service.ICityDirectoryService;

/**
 * 城市编码Service业务层处理
 * 
 * @author love
 * @date 2025-10-12
 */
@Service
public class CityDirectoryServiceImpl implements ICityDirectoryService 
{
    @Autowired
    private CityDirectoryMapper cityDirectoryMapper;

    @Autowired
    private SqlSession sqlSession;

    /**
     * 查询城市编码
     * 
     * @param id 城市编码主键
     * @return 城市编码
     */
    @Override
    public CityDirectory selectCityDirectoryById(String id)
    {
        return cityDirectoryMapper.selectCityDirectoryById(id);
    }

    /**
     * 查询城市编码列表
     * 
     * @param cityDirectory 城市编码
     * @return 城市编码
     */
    @Override
    public List<CityDirectory> selectCityDirectoryList(CityDirectory cityDirectory)
    {
        return cityDirectoryMapper.selectCityDirectoryList(cityDirectory);
    }

    /**
     * 新增城市编码
     * 
     * @param cityDirectory 城市编码
     * @return 结果
     */
    @Override
    public int insertCityDirectory(CityDirectory cityDirectory)
    {
        return cityDirectoryMapper.insertCityDirectory(cityDirectory);
    }

    /**
     * 修改城市编码
     * 
     * @param cityDirectory 城市编码
     * @return 结果
     */
    @Override
    public int updateCityDirectory(CityDirectory cityDirectory)
    {
        return cityDirectoryMapper.updateCityDirectory(cityDirectory);
    }

    /**
     * 批量删除城市编码
     * 
     * @param ids 需要删除的城市编码主键
     * @return 结果
     */
    @Override
    public int deleteCityDirectoryByIds(String[] ids)
    {
        return cityDirectoryMapper.deleteCityDirectoryByIds(ids);
    }

    /**
     * 删除城市编码信息
     * 
     * @param id 城市编码主键
     * @return 结果
     */
    @Override
    public int deleteCityDirectoryById(String id)
    {
        return cityDirectoryMapper.deleteCityDirectoryById(id);
    }

    @Override
    public List<CityNodeDTO> getCityCascaderData() {
        // 1. 获取 Mapper，执行查询（调用原有 selectCityDirectoryList 方法）
        CityDirectoryMapper mapper = sqlSession.getMapper(CityDirectoryMapper.class);
        List<CityDirectory> allCities = mapper.selectCityDirectoryList(new CityDirectory());

        // 2. 转换为级联选择器需要的格式（省 -> 市 -> 区）
        // 先筛选出所有“省份”（type = 1，parentCode 为 0 或 null）
        List<CityDirectory> provinces = allCities.stream()
                .filter(city -> city.getType() == 1 && (city.getParentCode() == null || "0".equals(city.getParentCode())))
                .collect(Collectors.toList());

        // 3. 递归构建级联结构
        return provinces.stream().map(province -> {
            CityNodeDTO provinceNode = new CityNodeDTO();
            provinceNode.setValue(province.getCityCode()); // 城市编码作为 value
            provinceNode.setLabel(province.getCityName()); // 城市名称作为 label

            // 筛选该省份下的“城市”（type = 2，parentCode 是省份的 cityCode）
            List<CityDirectory> cities = allCities.stream()
                    .filter(city -> city.getType() == 2 && province.getCityCode().equals(city.getParentCode()))
                    .collect(Collectors.toList());

            // 给省份节点设置“城市”子节点
            provinceNode.setChildren(cities.stream().map(city -> {
                CityNodeDTO cityNode = new CityNodeDTO();
                cityNode.setValue(city.getCityCode());
                cityNode.setLabel(city.getCityName());

                // 筛选该城市下的“区县”（type = 3，parentCode 是城市的 cityCode）
                List<CityDirectory> areas = allCities.stream()
                        .filter(area -> area.getType() == 3 && city.getCityCode().equals(area.getParentCode()))
                        .collect(Collectors.toList());

                // 给城市节点设置“区县”子节点
                cityNode.setChildren(areas.stream().map(area -> {
                    CityNodeDTO areaNode = new CityNodeDTO();
                    areaNode.setValue(area.getCityCode());
                    areaNode.setLabel(area.getCityName());
                    return areaNode;
                }).collect(Collectors.toList()));

                return cityNode;
            }).collect(Collectors.toList()));

            return provinceNode;
        }).collect(Collectors.toList());
    }

    @Override
    public List<HashMap<String, String>> selectProvinces() {
        return cityDirectoryMapper.selectProvinces();
    }

    @Override
    public List<HashMap<String, String>> selectCitys(String provinceCode) {
        return cityDirectoryMapper.selectCitys(provinceCode);
    }
}
